from time import time
from datetime import datetime

class Times:
    @staticmethod
    def test(name, task):
        if task is None:
            return
        name = f"【{name}】" if name else ""
        print(name)
        print(f"开始：{datetime.now().strftime('%H:%M:%S.%f')[:-3]}")
        begin = time() * 1000  # 转换为毫秒
        task()
        end = time() * 1000    # 转换为毫秒
        print(f"结束：{datetime.now().strftime('%H:%M:%S.%f')[:-3]}")
        delta = (end - begin) / 1000.0
        print(f"耗时：{delta}秒")
        print("-------------------------------------")

# 使用示例
if __name__ == "__main__":
    # 定义一个测试任务
    def my_task():
        total = 0
        for i in range(1000000):
            total += i
        print(f"计算结果: {total}")

    # 执行测试
    Times.test("性能测试", my_task)